// pages/scan/index.js
const app = getApp(); // 获取应用实例

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bomMenus: [], //底部菜单数据
    bomMenuId: 5, //页面ID
    bomMenuKd: '19%', //菜单宽度
    bdimg: 'https://shequ.freecode.top/upload/20250525/17481673616832eac1a9f68.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.bomMenuList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 底部菜单
  bomMenuList: function () {
    // this.setData({ bomMenus: app.globalData.bomAry }); return
    var _this = this;
    wx.request({
      method: 'post',
      url: app.globalData.xcxApi + 'init',
      data: {},
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        console.log(res.data);
        res = res.data;
        if (res.code < 0) {
          return
        }
        _this.setData({
          bomMenus: res.data.menus,
          bomMenuKd: res.data.kd,
        });
      },
      error: function (err) {
        console.log(err);
      }
    })
  },

  // 页面跳转
  toView: function(e) {
    console.log(e);
    var id = e.currentTarget.dataset.id;
    if (id == this.data.bomMenuId) { return }
    var bms = this.data.bomMenus, mlink = '', i = 0;
    for (; i < bms.length; i ++) {
      if (bms[i].id == id) {
        mlink = bms[i].link;
      }
    }
    console.log(mlink);
    // wx.navigateBack({
    //   url: mlink,
    // });
    wx.navigateTo({
      url: mlink,
    });
  },

	// 扫码添加
	toAdd: function (e) {
		var that = this;
		wx.scanCode({
			success: function (res) {
				console.log(res.result);
				var code = that.getParamByUrl({url: res.result, key: 'c'});
				console.log(code); //return
				wx.request({
					method: 'POST',
					url: app.globalData.xcxApi + 'scan/code/one',
					data: { code: code },
					timeout: 10000, //接口超时10S
					success: function (res) {
						res = res.data;
						console.log(res); //return
						if (res.code < 0) {
							wx.showToast({
								title: res.msg, icon: 'none'
							}); return
            }
            var pa = '?show_code=' + res.data.show_code;
            pa += '&code=' + code;
						wx.navigateTo({
							url: '/pages/scan/add/index' + pa,
						}); return
					},
					fail: function () {
						console.log('error');
					}
				});
			}
		});
	},

	// 扫码修改
	toEdit: function (e) {
		var that = this;
		wx.scanCode({
			success: function (res) {
				console.log(res.result);
				var code = that.getParamByUrl({url: res.result, key: 'c'});
				console.log(code); //return
				wx.request({
					method: 'POST',
					url: app.globalData.xcxApi + 'scan/code/one',
					data: { code: code },
					timeout: 10000, //接口超时10S
					success: function (res) {
						res = res.data;
						console.log(res); //return
						if (res.code < 0) {
							wx.showToast({
								title: res.msg, icon: 'none'
							}); return
            }
            var pa = '?show_code=' + res.data.show_code;
            pa += '&code=' + code;
            var type = res.data.yw_type, tname = '';
            if (type == 1) {
              tname = 'tmp';
            } else if (type == 2) {
              tname = 'goods';
            } else if (type == 3) {
              tname = 'fuwu';
            } else {
              wx.showToast({
                title: '类型错误', icon: 'none'
              }); return
            }
						wx.navigateTo({
							url: '/pages/scan/' + tname + '/index' + pa,
						}); return
					},
					fail: function () {
						console.log('error');
					}
				});
			}
		});
  },
  
  // 编号搜索
  searchByCode: function (e) {
    wx.navigateTo({
      url: '/pages/scan/serial/index',
    });
  },

	// 从URL中获取参数
	getParamByUrl: function (e) {
    var httpUrls = e.url.split('?');
    console.log(app.globalData.h5Domain);
    console.log(httpUrls);
    if (httpUrls[0] != app.globalData.h5Domain) {
      wx.showToast({
        title: '非法二维码', icon: 'none'
      }); return
    }
    if (httpUrls[1] == '') {
      wx.showToast({
        title: '没有参数', icon: 'none'
      }); return
    }
    // console.log(httpUrls[1]);
    var params = httpUrls[1].split('&');
    var i = 0, kvs = '', val = '';
    for (; i < params.length; i ++) {
      kvs = params[i].split('=');
      if (kvs[0] == e.key) {
        val = kvs[1];
      }
    }
    return val;
	},
})